Skip to main content
Version: 1.0.0

sendQuotationEmail

Email Template​

Quotation Email Template

Function Name: sendQuotationEmail

Author: Domenico Cerone Creation Date: 02/10/2025
Last Reviewer: Domenico Cerone

Trigger: HTTPS (onRequest)

Purpose: Sends quotation emails for catalog orders with attachments downloaded from Firebase Storage. This Cloud Function retrieves order data, downloads the latest quotation file, and sends it via ZeptoMail with a professional email template.

Detailed Functionality​

1. INPUT VALIDATION AND PARAMETERS​

  • Required Parameter: orderId - ID of the document in 'CatalogOrders' collection
  • Validates that orderId is provided and not empty
  • Accepts only POST requests
  • Returns appropriate HTTP status codes for different error scenarios

2. CATALOG ORDER DATA RETRIEVAL​

  • Queries 'CatalogOrders' collection using the provided orderId
  • Validates that the order document exists
  • Extracts profile (recipient email), batch (optional batch identifier), and list_quote_data (array of quotation objects)
  • Validates that both properties exist and list_quote_data is a non-empty array

3. QUOTATION DATA SELECTION​

  • Takes the last element from list_quote_data array (most recent quotation)
  • Extracts quoteUrl (Firebase Storage URL), quoteName, and quoteNotes
  • Validates that quoteUrl exists and is a valid string
  • Handles cases where quotation data is missing or invalid

4. NOTIFICATION PREFERENCE CHECK​

  • Checks if the user has enabled the sendQuotationEmail notification in their notification preferences
  • Verifies notification_types.sendQuotationEmail field in the profile
  • If notification is disabled (false): returns success without sending email
  • If notification is enabled (true): proceeds with file download from Firebase Storage
  • Provides clear logging about notification preference status

5. FILE DOWNLOAD FROM FIREBASE STORAGE​

  • Downloads the file from the Firebase Storage URL
  • Converts file to base64 format for email attachment
  • Automatic MIME type detection based on file extension
  • 30-second timeout to prevent network blocks
  • Supports multiple file formats: PDF, Excel, Word, images, text files

6. MIME TYPE DETECTION​

  • PDF: application/pdf
  • Excel: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (xlsx) / application/vnd.ms-excel (xls)
  • Word: application/vnd.openxmlformats-officedocument.wordprocessingml.document (docx) / application/msword (doc)
  • Images: image/jpeg (jpg/jpeg) / image/png (png)
  • Text: text/plain (txt)
  • Default: application/octet-stream

7. EMAIL TEMPLATE CONFIGURATION​

  • Uses ZeptoMail as the primary email service
  • Template Key: 13ef.8598f19fbcc5adb.k1.b1523170-9d25-11f0-9d65-dad70ff08860.199952fde07
  • Sender: "no-reply@arshades.com" (noreply)
  • Professional template with quotation details and attachment

8. TEMPLATE VARIABLES​

  • {{quoteName}}: Name of the quotation
  • {{quoteNotes}}: Additional notes about the quotation
  • {{batch}}: Batch identifier (optional)
  • {{orderId}}: Order ID for reference

9. EMAIL ATTACHMENT HANDLING​

  • Converts downloaded file to base64 format
  • Attaches file with proper MIME type and filename
  • Preserves original filename from Firebase Storage URL
  • Handles large files with appropriate timeout settings

10. ERROR HANDLING AND VALIDATION​

  • Comprehensive validation at each step
  • Specific error messages for missing data
  • Graceful handling of download failures
  • Detailed logging for debugging purposes
  • Proper HTTP status codes for different error scenarios

11. RESPONSE STRUCTURE​

  • Success response includes all processed data
  • Attachment information
  • Template variables used
  • ZeptoMail response details
  • Detailed error information on failure

Input (Payload)​

Method: POST

Headers:

  • Content-Type: application/json

Body:

{
"orderId": "5AdIae1rNiSfaRa1HyyL"
}

Parameters:

  • orderId (string, required): Unique identifier of the catalog order document

Output (Success)​

{
"success": true,
"message": "Email di quotazione inviata con successo",
"recipientEmail": "user@example.com",
"attachmentFileName": "quote_1727618879438.pdf",
"quoteName": "Quote Q1 2025",
"quoteNotes": "Delivery expected in 15 working days...",
"batch": "BATCH_001",
"emailResponse": {
"data": {
"message": "Email sent successfully"
}
}
}

Response Properties:

  • success (boolean): Indicates if the operation was completed successfully
  • message (string): Descriptive message about the operation result
  • recipientEmail (string): Email address of the recipient
  • attachmentFileName (string): Name of the attached file
  • quoteName (string): Name of the quotation
  • quoteNotes (string): Additional notes about the quotation
  • batch (string): Batch identifier (optional)
  • emailResponse (object): Response from ZeptoMail service

Output (Error)​

{
"success": false,
"error": "Ordine con ID 5AdIae1rNiSfaRa1HyyL non trovato nella collezione CatalogOrders"
}

Common Error Scenarios:

  • Missing orderId parameter
  • Order not found in CatalogOrders collection
  • Missing profile or list_quote_data properties
  • Empty list_quote_data array
  • Invalid quoteUrl in quotation data
  • File download failure from Firebase Storage
  • ZeptoMail service errors

Firestore Data Structure​

The CatalogOrder document must contain:

  • profile: recipient email address
  • batch: batch identifier (optional)
  • list_quote_data: array of quotation objects with structure:
    {
    "quoteUrl": "https://firebasestorage.googleapis.com/...",
    "quoteName": "Quote Q1 2025",
    "quoteNotes": "Delivery expected in 15 working days...",
    "quoteDate": "2025-01-15T00:00:00.000Z",
    "timestamp": "2025-09-29T13:27:59.438Z",
    "uploadedBy": "admin@arshades.com",
    "fileName": "quote_1727618879438.pdf"
    }

Testing​

URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/europe-central2/sendQuotationEmail

Test with Emulator:

  1. Start Firebase emulators:

    firebase emulators:start --only functions
  2. Set Node.js version:

    nvm use 20
  3. Test with curl:

    curl -X POST "http://127.0.0.1:5001/arshadesstaging/europe-central2/sendQuotationEmail" \
    -H "Content-Type: application/json" \
    -d '{"orderId": "5AdIae1rNiSfaRa1HyyL"}'

Postman Testing:

  • Method: POST
  • URL: http://127.0.0.1:5001/arshadesstaging/europe-central2/sendQuotationEmail
  • Headers:
    • Key: Content-Type
    • Value: application/json
  • Body: raw JSON (see examples above)

Deploy Command​

firebase deploy --only functions:sendQuotationEmail

Production URL​

Live Function: https://europe-central2-arshades-7e18a.cloudfunctions.net/sendQuotationEmail

MAIL_TEMPLATE_KEY​

  • Quotation Email: 13ef.8598f19fbcc5adb.k1.b1523170-9d25-11f0-9d65-dad70ff08860.199952fde07